package com.cskaoyan._02interface._05example.dao;

import com.cskaoyan._02interface._05example.model.Product;
import com.cskaoyan._02interface._05example.model.ProductData;

/**
 * @description:
 * @author: 景天
 * @date: 2023/2/11 12:00
 **/

public class ProductDaoImpl implements ProductDao {
    // 定义个成员变量
    private Product[] products = ProductData.PRODUCTS;

    @Override
    public Product[] getRealProducts() {
        int count = 0;
        // 遍历数组
        for (Product product : products) {
            // 统计null的个数
            if (product == null) {
                count++;
            }
        }

        // 去出null
        // 创建新数组
        Product[] arr = new Product[this.products.length - count];
        // 数组下标
        int index = 0;
        for (Product product : products) {
            if (product == null) {
                continue;
            }
            // 重新赋值
            arr[index++] = product;
        }
        // 返回新数组

        return arr;
    }
}
